<template>
  <div class="login-container">
    <div class="login-wrapper">
      <a-card class="login-card">
        <div class="login-header">
          <div class="login-icon">
            <icon-user class="icon" />
          </div>
          <h2 class="login-title">欢迎回来</h2>
          <p class="login-subtitle">请登录您的账户</p>
        </div>

        <a-form ref="loginFormRef" :model="loginForm" :rules="formRules" class="login-form">
          <div class="input-section">
            <div class="input-label">
              <icon-mobile />
              <span>手机号</span>
            </div>
            <a-form-item field="phone" hide-label>
              <a-input 
                v-model="loginForm.phone" 
                placeholder="请输入手机号"
                size="large"
                :max-length="11"
                allow-clear
              />
            </a-form-item>
          </div>

          <div class="input-section">
            <div class="input-label">
              <icon-lock />
              <span>密码</span>
            </div>
            <a-form-item field="password" hide-label>
              <a-input-password 
                v-model="loginForm.password" 
                placeholder="请输入密码"
                size="large"
                :max-length="20"
                allow-clear
              />
            </a-form-item>
          </div>

          <div class="button-section">
            <a-button 
              type="primary" 
              size="large" 
              :loading="loading"
              :disabled="loading"
              @click="handleLogin"
              class="login-button"
            >
              <template #icon>
                <icon-export />
              </template>
              立即登录
            </a-button>
          </div>
        </a-form>

        <div class="register-section">
          <p>还没有账户？</p>
          <a-button 
            type="outline" 
            size="medium"
            @click="goToRegister"
            class="register-button"
          >
            <template #icon>
              <icon-user-add />
            </template>
            立即注册
          </a-button>
        </div>

        <div class="login-footer">
          <a-link @click="goToForgotPassword" class="forgot-password-link">
            忘记密码？
          </a-link>
        </div>
      </a-card>

      <div class="login-decoration">
        <div class="decoration-circle circle-1"></div>
        <div class="decoration-circle circle-2"></div>
        <div class="decoration-circle circle-3"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user.js'
import { userAPI } from '@/api/user/user.js'
import { Message } from '@arco-design/web-vue'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref()

const loginForm = reactive({
  phone: '',
  password: ''
})

const loading = ref(false)

const formRules = {
  phone: [
    { required: true, message: '请输入手机号' },
    {
      validator: (value, callback) => {
        if (!/^1[3-9]\d{9}$/.test(value)) {
          callback('请输入正确的手机号')
        } else {
          callback()
        }
      }
    }
  ],
  password: [
    { required: true, message: '请输入密码' }
  ]
}

const handleLogin = async () => {
  // 表单验证
  const validate = await loginFormRef.value.validate()
  if (validate) return

  try {
    loading.value = true

    const response = await userAPI.login({
      phone: loginForm.phone,
      password: loginForm.password
    })

    console.log(response)

    if (response.code == 200) {
      // 保存用户信息到store（store会自动保存到localStorage）
      userStore.setUserInfo(response.data || response)
      
      // 显示成功消息
      Message.success('登录成功！')

      // 跳转到首页
      router.push('/search')
    } else {
      Message.error('登录失败，请检查账号密码')
    }
  } catch (err) {
    Message.error(err.response?.data?.message || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}

const goToForgotPassword = () => {
  Message.info('密码重置功能开发中...')
}

// 回车键登录事件处理
const handleEnterLogin = (event) => {
  if (event.key === 'Enter' && !loading.value) {
    event.preventDefault() // 阻止默认行为
    // 直接调用登录函数，让handleLogin处理验证
    handleLogin()
  }
}

// 添加键盘事件监听
onMounted(() => {
  document.addEventListener('keydown', handleEnterLogin)
})

// 移除键盘事件监听
onUnmounted(() => {
  document.removeEventListener('keydown', handleEnterLogin)
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #165dff 0%, #14c9c9 50%, #722ed1 100%);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

@keyframes gradientBG {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}

.login-wrapper {
  position: relative;
  z-index: 2;
  padding: 20px;
  max-width: 100%;
  box-sizing: border-box;
}

.login-card {
  width: 480px;
  max-width: 100%;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin: 0 auto;
}

.login-header {
  text-align: center;
  padding: 32px 32px 24px;
}



.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #1d2129;
  margin-bottom: 8px;
  background: linear-gradient(135deg, #165dff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.login-subtitle {
  font-size: 14px;
  color: #86909c;
  margin: 0;
}



.input-section {
  margin-bottom: 20px;
}

.input-label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  color: #1d2129;
  font-weight: 500;
  font-size: 14px;
}



.button-section {
  margin-top: 24px;
  margin-bottom: 20px;
}

.login-button {
  background: linear-gradient(135deg, #4f46e5, #7c3aed, #ec4899);
  border: none;
  font-weight: 600;
  font-size: 16px;
  height: 48px;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 15px rgba(79, 70, 229, 0.3);
  position: relative;
  overflow: hidden;
  width: 100%;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 30px rgba(124, 58, 237, 0.4);
}

.login-button:hover::before {
  left: 100%;
}

.login-button:active {
  transform: translateY(-1px);
}

.register-section {
  text-align: center;
  padding: 24px 32px;
  background: rgba(247, 248, 250, 0.8);
  border-top: 1px solid #e5e6eb;
}

.register-section p {
  margin: 0 0 12px 0;
  color: #86909c;
  font-size: 14px;
}

.register-button {
  background: linear-gradient(135deg, #06b6d4, #3b82f6, #8b5cf6);
  border: none;
  font-weight: 600;
  font-size: 14px;
  height: 40px;
  border-radius: 10px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3);
  color: white;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
  background: linear-gradient(135deg, #0891b2, #2563eb, #7c3aed);
}

.login-footer {
  text-align: center;
  padding: 16px;
}

.forgot-password-link {
  color: #7c3aed;
  font-weight: 500;
  transition: all 0.3s ease;
  text-decoration: none;
}

.forgot-password-link:hover {
  color: #ec4899;
  text-shadow: 0 0 8px rgba(236, 72, 153, 0.3);
}

.login-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.circle-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.circle-3 {
  width: 60px;
  height: 60px;
  bottom: 20%;
  left: 20%;
  animation-delay: 4s;
}

@keyframes float {

  0%,
  100% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-20px);
  }
}

@media (max-width: 480px) {
  .login-wrapper {
    padding: 10px;
  }
  
  .login-card {
    width: 100%;
    margin: 0;
    border-radius: 12px;
  }
  
  .login-header {
    padding: 20px 20px 12px;
  }
  
  .login-title {
    font-size: 22px;
  }
  

  
  .register-section {
    padding: 16px 20px;
  }
  
  .login-footer {
    padding: 10px 20px;
  }
}
</style>